package exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import exam.bean.*;
import exam.dao.FinanceInfoDao;
import exam.dao.ProjectInfoDao;
import exam.dao.UserInfoDao;
import exam.dao.UtilFileDao;
import exam.service.IFinanceInfoService;
import exam.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FinanceInfoServiceImpl  implements IFinanceInfoService {

    @Autowired
    FinanceInfoDao financeInfoDao;

    @Autowired
    UtilFileDao utilFileDao;

    @Autowired
    ProjectInfoDao projectInfoDao;


    @Autowired
    UserInfoDao userInfoDao;

    @Override
    public PageInfo<FinanceInfo> selectInfo(int pageNum, int pageSize, Map<String, Object> queryMap) {
        PageHelper.startPage(pageNum,pageSize);
        UserInfo userInfo = getCurrentUserInfo();
        if(userInfo.getUserGroupId() != 1){
            queryMap.put("enterUserId",userInfo.getId());
        }
        return new PageInfo<>(financeInfoDao.selectInfo(queryMap));
    }

    @Override
    public FinanceInfo selectInfoSingle(int id) {
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("id",id);
        List<FinanceInfo> relist = financeInfoDao.selectInfo(queryMap);
        if(relist == null || relist.size() == 0){
            return null;
        }else {
            FinanceInfo financeInfo = relist.get(0);
            Map<String,Object> map = new HashMap<>();
            map.put("utilType",5);
            map.put("dataId",queryMap.get("id"));
            financeInfo.setFiles(utilFileDao.selectInfo(map));
            return financeInfo;
        }
    }

    @Override
    public int insertInfo(FinanceInfo financeInfo,String uploadPath) {
        //获取当前时间
        financeInfo.setUpdateTime(new Date());

        int inInfo = financeInfoDao.insertInfo(financeInfo);
        int fileRe = 0;
        if(inInfo > 0 && financeInfo.getUtilFiles() != null){
            for (MultipartFile file:financeInfo.getUtilFiles()) {
                UtilFiles files = new UtilFiles();
                files.setDataId(financeInfo.getId());
                files.setUtilName(FileUtil.upLoadFileSingle(file,uploadPath));
                files.setUtilAddress(uploadPath);
                files.setUtilSrcName(file.getOriginalFilename());
                files.setUtilFileType(file.getContentType());
                files.setUtilType(5);
                fileRe = utilFileDao.insertInfo(files);
            }
        }

        //提交类型判断 =>  0: 提交 -->  余额修改   1: 保存 -> 余额不进行修改
        if(inInfo > 0 && financeInfo.getCurrentStatus() == 0){
            Map<String,Object> queryMap = new HashMap<>();
            queryMap.put("id",financeInfo.getProjectId());
            //获得当前项目情况
            ProjectInfo projectInfo = projectInfoDao.selectInfo(queryMap).get(0);

            //财务类型判断
            if("入账".equals(financeInfo.getFinanceType())){
                //余额修改
                projectInfo.setRemainPrice(
                        projectInfo.getRemainPrice() + financeInfo.getFinanceTotal()
                );
                //总金额修改
                projectInfo.setTotalPrice(projectInfo.getTotalPrice() + financeInfo.getFinanceTotal());

            }else if("支出".equals(financeInfo.getFinanceType())){
                projectInfo.setRemainPrice(
                        projectInfo.getRemainPrice() - financeInfo.getFinanceTotal()
                );
            }
            int re = projectInfoDao.updateInfo(projectInfo);

            if(re == 0){
                return re;
            }
        }

        return inInfo  + fileRe;
    }

    @Override
    public int updateInfo(FinanceInfo financeInfo,String uploadPath) {
        financeInfo.setUpdateTime(new Date());
        //附件添加
        int fileRe = 0;
        if(financeInfo.getUtilFiles() != null){
            for (MultipartFile file:financeInfo.getUtilFiles()) {
                UtilFiles files = new UtilFiles();
                files.setDataId(financeInfo.getId());
                files.setUtilName(FileUtil.upLoadFileSingle(file,uploadPath));
                files.setUtilAddress(uploadPath);
                files.setUtilSrcName(file.getOriginalFilename());
                files.setUtilFileType(file.getContentType());
                files.setUtilType(5);
                fileRe = utilFileDao.insertInfo(files);
            }
        }

        //提交类型判断 =>  0: 提交 -->  余额修改   1: 保存 -> 余额不进行修改
        if(financeInfo.getCurrentStatus() == 0){

            Map<String,Object> queryMap = new HashMap<>();
            queryMap.put("id",financeInfo.getProjectId());
            //获得当前项目情况
            ProjectInfo projectInfo = projectInfoDao.selectInfo(queryMap).get(0);

            //财务类型判断
            if("入账".equals(financeInfo.getFinanceType())){
                //余额修改
                projectInfo.setRemainPrice(
                        projectInfo.getRemainPrice() + financeInfo.getFinanceTotal()
                );
                //总金额修改
                projectInfo.setTotalPrice(projectInfo.getTotalPrice() + financeInfo.getFinanceTotal());

            }else if("支出".equals(financeInfo.getFinanceType())){
                projectInfo.setRemainPrice(
                        projectInfo.getRemainPrice() - financeInfo.getFinanceTotal()
                );
            }
            int re = projectInfoDao.updateInfo(projectInfo);

            if(re == 0){
                return re;
            }
        }


        return financeInfoDao.updateInfo(financeInfo);
    }

    @Override
    public int deleteInfo(int id) {
        //查询详细信息 ->  判断是否为提交类型
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("id",id);
        List<FinanceInfo>  relist = financeInfoDao.selectInfo(queryMap);
        if(relist == null || relist.size() == 0){
            return 0;
        }
        FinanceInfo financeInfo = relist.get(0);
        if(financeInfo.getCurrentStatus() == 0) {
            return 0;
        }
        return financeInfoDao.deleteInfo(id);
    }


    public UserInfo getCurrentUserInfo(){
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        Map<String, Object> map = new HashMap<>();
        map.put("name",authenticationToken.getPrincipal());
        return userInfoDao.selectInfo(map).get(0);
    }
}
